package com.central.scheduler.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.common.model.WorkerServer;
import com.central.common.service.impl.SuperServiceImpl;
import com.central.scheduler.api.service.IWorkerGroupService;
import com.central.scheduler.dao.mapper.ProWorkerMapper;
import com.central.scheduler.dao.mapper.WorkerGroupMapper;
import com.central.scheduler.dao.mapper.WorkerServerMapper;
import com.central.scheduler.dao.model.ProWorker;
import com.central.scheduler.dao.model.WorkerGroup;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @author Tindy
 * @date 2021/6/24
 * @describe
 */
@Slf4j
@Service
public class WorkerGroupServiceImpl extends SuperServiceImpl<WorkerGroupMapper, WorkerGroup> implements IWorkerGroupService {

    @Autowired
    private ProWorkerMapper proWorkerMapper;
    @Autowired
    private WorkerServerMapper workerServerMapper;

    @Override
    public Result addOrUpdateWorkerGroup(Long proId, WorkerGroup workerGroup) {
        //检查 工作组为当前项目 和 工作组节点是否都归属项目
        if(proId==-1l || (checkPermi(proId,workerGroup) && checkWorkerIdsIsPro(proId,workerGroup.getWorkerIds()))){
            List<WorkerServer> workerServers = workerServerMapper.selectBatchIds(workerGroup.getWorkerIds());
            workerGroup.setWorkerServers(workerServers);
            saveOrUpdate(workerGroup);
            return Result.succeed("操作成功");
        }else {
            return Result.failed("操作失败，权限不足");
        }
    }

    private boolean checkPermi(Long proId, WorkerGroup workerGroup) {
        if(workerGroup.getId()!=null){
            WorkerGroup workerGroup1 = getById(workerGroup.getId());
            if(proId.equals(workerGroup1.getProId())) return true;
            else return false;
        }else{
            workerGroup.setProId(proId);
            return true;
        }
    }

    @Override
    public void delWorkerGroup(Long id, Long proId) {
        remove(new QueryWrapper<WorkerGroup>().eq("id",id).eq("pro_id",proId));
    }

    @Override
    public PageResult<WorkerGroup> findWorkerGroupsByPro(Map<String, Object> params) {
        Page<WorkerGroup> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<WorkerGroup> list = baseMapper.findWorkerGroups(page, params);
        long total = page.getTotal();
        return PageResult.<WorkerGroup>builder().data(list).code(0).count(total).build();
    }

    private boolean checkWorkerIdsIsPro(Long proId, List<Long> workerIds) {
        Set<Long> ids= proWorkerMapper.getIdsByProId(proId);
        return ids.containsAll(workerIds);
    }
}
